<?php
	class ProfileView
	{
		// Member Variables
		private $_navView = null;
		
		// Constants
		const OPTIONSTEXTAREACOLS = 64;
		const OPTIONSTEXTAREAROWS = 4;
		const MEMBER = "Member";
		const SHOWEMAIL = "Showmail";
		const SHOWGIVENNAME = "Showgiven";
		const SHOWSURNAME = "Showsur";
		const SHOWBIRTHDAY = "Birthday";
		const SHOWLOCATION = "Location";
		const SHOWGENDER = "Gender";
		const SHOWPERSONALTEXT = "PersonalText";
		
		public function __construct(\NavigationView $navView)
		{
			$this->_navView = $navView;
		}

		// Returns the options form.
		public function DoProfileOptionsForm($profile)
		{
			$ret = "";
			
			// If the profile exists return the profileoptions with saved options filled in.
			if ($profile != null)
			{			
				$ret = "<form method='post' action='" . $this->_navView->GetOptionAdress() . "'>";
					// Show mail
					if ($profile->showmail == 1)
					{
						$ret .= "Show mail-adress:<input id='showmail' name='" . ProfileView::SHOWEMAIL . "' type='checkbox' value='1' checked='yes' /><br />";
					}
					else 
					{
						$ret .= "Show mail-adress:<input id='showmail' name='" . ProfileView::SHOWEMAIL . "' type='checkbox' value='1' /><br />";
					}
					// Show given
					if ($profile->showgiven == 1)
					{
						$ret .= "Show given name:<input id='showgiven' name='" . ProfileView::SHOWGIVENNAME . "' type='checkbox' value='1' checked='yes' /><br />";
					}
					else 
					{
						$ret .= "Show given name:<input id='showgiven' name='" . ProfileView::SHOWGIVENNAME . "' type='checkbox' value='1' /><br />";
					}
					// Show sur
					if ($profile->showsur == 1)
					{
						$ret .= "Show surname:<input id='showsur' name='" . ProfileView::SHOWSURNAME . "' type='checkbox' value='1' checked='yes' /><br />";
					}
					else 
					{
						$ret .= "Show surname:<input id='showsur' name='" . ProfileView::SHOWSURNAME . "' type='checkbox' value='1' /><br />";
					}
					$ret .= "<br />" .
					"<u>Personal info</u><br />" .
					"<b>Warning:</b> If you input any personal info it will be shown. <br />" .
					"If you don't want to show it just leave these fields blank. <br />";
					// Show birthday
					if ($profile->birthday != "" && $profile->birthday != null)
					{
						$ret .= "Birthday: <input id='birthday' name='" . ProfileView::SHOWBIRTHDAY . "' type='text' value='$profile->birthday' /> yyyy-mm-dd<br />";
					}
					else 
					{
						$ret .= "Birthday: <input id='birthday' name='" . ProfileView::SHOWBIRTHDAY . "' type='text' /> yyyy-mm-dd<br />";
					}
					// Show location
					if ($profile->location != "" && $profile->location != null)
					{
						$ret .= "Location: <input id='locaion' name='" . ProfileView::SHOWLOCATION . "' type='text' value='$profile->location' /><br />";
					}
					else 
					{
						$ret .= "Location: <input id='locaion' name='" . ProfileView::SHOWLOCATION . "' type='text' /><br />";
					}
					// Show gender
					if($profile->gender == 1)
					{
						$ret .= "Gender: " . "<select id='gender' name='" . ProfileView::SHOWGENDER . "'>" .
							"<option value='1'>Male</option>" .
							"<option value='2'>Female</option>" .
							"<option value='0'>Hide</option>" .
						"</select>" .
						"<br />";
					}
					elseif($profile->gender == 2)
					{
						$ret .= "Gender: " . "<select id='gender' name='" . ProfileView::SHOWGENDER . "'>" .
							"<option value='2'>Female</option>" .
							"<option value='1'>Male</option>" .
							"<option value='0'>Hide</option>" .
						"</select>" .
						"<br />";
					}
					else 
					{
						$ret .= "Gender: " . "<select id='gender' name='" . ProfileView::SHOWGENDER . "'>" .
							"<option value='0'>Hide</option>" .
							"<option value='1'>Male</option>" .
							"<option value='2'>Female</option>" .
						"</select>" .
						"<br />";
					}
					// Show personal text
					if ($profile->personaltext != "" && $profile->personaltext != null)
					{
						$ret .= "<textarea rows='" . $this::OPTIONSTEXTAREAROWS . "' cols='" . $this::OPTIONSTEXTAREACOLS . "' name='" . ProfileView::SHOWPERSONALTEXT . "'/>$profile->personaltext</textarea><br />";
					}
					else 
					{
						$ret .= "<textarea rows='" . $this::OPTIONSTEXTAREAROWS . "' cols='" . $this::OPTIONSTEXTAREACOLS . "' name='" . ProfileView::SHOWPERSONALTEXT . "'></textarea><br />";
					}
					$ret .= "<input id='save' name='" . NavigationView::IWANTTOSAVEOPTIONS . "' type='submit' value='Save' />" .
				"</form>";
			}
			else // Return a blank form.
			{
				$ret = "<form method='post' action='" . $this->_navView->GetOptionAdress() . "'>" .
					"Show mail-adress:<input id='showmail' name='" . ProfileView::SHOWEMAIL . "' type='checkbox' value='1' /><br />" .
					"Show given name:<input id='showgiven' name='" . ProfileView::SHOWGIVENNAME . "' type='checkbox' value='1' /><br />" .
					"Show surname:<input id='showsur' name='" . ProfileView::SHOWSURNAME . "' type='checkbox' value='1' /><br />" .
					"<br />" .
					"<u>Personal info</u><br />" .
					"<b>Warning:</b> If you input any personal info it will be shown. <br />" .
					"If you don't want to show it just leave these fields blank. <br />" .
					"Birthday: <input id='birthday' name='" . ProfileView::SHOWBIRTHDAY . "' type='text' /> yyyy-mm-dd<br />" .
					"Location: <input id='locaion' name='" . ProfileView::SHOWLOCATION . "' type='text' /><br />" .
					"Gender: " . "<select id='gender' name='" . ProfileView::SHOWGENDER . "'>" .
						"<option value='0'>Male</option>" .
						"<option value='1'>Female</option>" .
					"</select>" .
					"<br />" .
					"<textarea rows='" . $this::OPTIONSTEXTAREAROWS. "' cols='" . $this::OPTIONSTEXTAREACOLS . "' name='" . ProfileView::SHOWPERSONALTEXT . "'/></textarea><br />" .
					"<input id='save' name='" . NavigationView::IWANTTOSAVEOPTIONS . "' type='submit' value='Save' />" .
				"</form>";
			}
			
			return $ret;
		}
		
		// Returns the formatted profile.
		public function PrintProfile($profile, \User $user)
		{
			$ret = "User: " . $user->username . "<br />";
			
			// If the profile isn't null check what options to show.
			if ($profile != null)
			{
				// Check if given should be shown.
				if ($profile->showgiven == 1)
				{
					$ret .= "Given name: " . $user->givenname . "<br />";
				}
				// Check if sur should be shown.
				if ($profile->showsur == 1)
				{
					$ret .= "Surname: " . $user->surname . "<br />";
				}
				// Check if mail should be shown.
				if ($profile->showmail == 1)
				{
					$ret .= "E-mail: " . $user->email . "<br />";
				}
				// Check if birthday should be shown.
				if ($profile->birthday != "" && $profile->birthday != null)
				{
					$ret .= "Birthday: " . $profile->birthday . "<br />";
				}
				// Check if location should be shown.
				if ($profile->location != "" && $profile->location != null)
				{
					$ret .= "Location: " . $profile->location . "<br />";
				}
				// Check if gender should be shown.
				if ($profile->gender == 1)
				{
					$ret .= "Gender: Male<br />";
				}
				elseif ($profile->gender == 2)
				{
					$ret .= "Gender: Female<br />";
				}
				// Check if personal text should be shown.
				if ($profile->personaltext != "" && $profile->personaltext != null)
				{
					$ret .= $profile->personaltext;
				}
			}		
			return $ret;
		}

		// Returns the profile adress for the current user.
		public function GenerateProfileAdress()
		{
			return $this->_navView->GetFormActionLink() . "?User=" . $_SESSION[UserView::USERNAME];
		}
		
		// Returns wether the user tried to load thier profile.
		public function TriedToLoadProfile()
		{
			if (isset($_GET[NavigationView::IWANTTOLOADAPROFILE]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether the user tried to load another members profile.
		public function TriedToLoadMemberProfile()
		{
			if (isset ($_GET[ProfileView::MEMBER]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether the user tried to load their options.
		public function TriedToCheckOptions()
		{
			if (isset($_GET[NavigationView::IWANTTOCHECKOPTIONS]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether the user tried to save their options.
		public function TreidToSaveOptions()
		{
			if (isset($_POST[NavigationView::IWANTTOSAVEOPTIONS]) == true)
			{
				return true;
			}
			return false;
		}
	}